package com.neulogistics.contrib.usercenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.usercenter.api.application.command.CreateUserCommand;
import com.neulogistics.contrib.usercenter.api.application.command.UpdateUserCommand;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.User;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserRole;
import com.neulogistics.contrib.usercenter.domain.repository.IUserRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateUserCommandHandler implements IRequestHandler<UpdateUserCommand, BaseResponse<Boolean>> {
    @Resource
    private IUserRepository userRepository;

    @Override
    public BaseResponse<Boolean> handle(UpdateUserCommand command) throws Exception {
        User byId = userRepository.findById(command.userId);
        if(byId == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        User user = new User();
        BeanUtils.copyProperties(command, user);

        // 持久化保存
        userRepository.save(user);
        return ResultUtils.success(Boolean.TRUE);
    }
}
